import Mock from "mockjs";
import {param2Obj} from '@/untial/urlsearch'
var List = [];

for (var i = 0; i < 100; i++) {
  
  List.push({
    id: Mock.mock("@guid()"),
    number:Mock.mock({
      "number|+1": 1
    }),
    name: Mock.mock("@cname()"),
    Personnelnumbers: Mock.mock({ "number|10000000-20000000": 20000000 }),
    power: Mock.mock("@natural(0, 1)"),
    type: Mock.mock("@natural(0, 1)"),
    newdate:Mock.mock('@datetime()'),
    effect: Mock.mock( "@natural(0, 1)" ),
    // operation: "",
  });
}

let arr = [];



export default {
  getHomeData: (config) => {
    var { keyword,page,pageSize,} = param2Obj(config.url)
    var res = List.filter(item =>item.name.indexOf(keyword) !== -1)
    
    
    console.log(res);
   
    var resLimit = res.filter((item,index)  =>{
      return (page-1) * pageSize  <= index && index < page * pageSize 
    } )

    console.log(config);
    return {
      code: 200,
      msg: "请求成功了",
      data: resLimit,
      total:List.length
    };
  },
  DeleteData: (config) => {
    var { id } = JSON.parse(config.body);
    var res = List.filter((item) => item.id != id);
    List = res;
    return {
      code: 200,
      msg: "删除成功",
      data: List,
    };
  },
  AddData:(config) => {
    
    const {name,effect,power,type,}=JSON.parse(config.body)
   List.unshift({
     id:Mock.mock("@guid()"),
     newdate: Mock.mock("@now()"),
     name:name,
     Personnelnumbers:Mock.mock({ "number|10000000-20000000": 20000000 }),
     effect:effect,
     power:power,
     type:type,
     number: Mock.mock({
      "number|+1": 1
    }), }

   )
  return {
      code: 200,
      msg: "添加成功了",
      data: null,
    };
  },
  editData:(config) => {
    const {id,name,effect,power,type,}=JSON.parse(config.body)
    List.forEach(item=>{
       if(item.id==id){
        item.name=name,
        item.effect=effect,
        item.power=power,
        item.type=type
       return
       }

    })
   
  return {
      code: 200,
      msg: "修改成功了",
      data: null,
    };
  },
};
